package DB;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import Data.FileData;
import Data.FileManager;

/**
 * Class to add new file to the DB
 * @author Erez
 *
 */
public class AddFile 
{
	private FileData file;
	public AddFile(FileData file) 
	{
		this.file=file;
		this.addToDB();
	}
	
	
    /**
     * adding a new file to DB
     * @param filename the new file name
     * @param tag the file tag
     * @param strategy the strategy of tagging
     * @param strategytype int that means user tag or system tag
     */

	public AddFile(String filename, String tag, String strategy , int strategytype) 
	{
		file=new FileData();
		file.setName(filename);
		file.setStrategy(strategy);
		file.setStrategyTag(strategytype);
		file.setTag(tag);
		this.addToDB();
		
	}
	
    /**
     * function that add the file to the DB
     * opens a session in the files DB and saves the file with the help of the FileManager
     * at the end the change is saved in an array of changes
     */

	//saving the new file in the database
	public synchronized void addToDB()
	{
		SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
		Session session = sessionFactory.openSession();
    	FileManager manager = new FileManager(session);

    	manager.saveFile(file);
    	System.out.println("file saved with ID = "+ file.getFileId());
    	session.flush();
	}
}
